(function () {

	//获取显示页面数据 服务器分页
    var serverData = {};
    var serverTable;
    jQuery(document).ready(function () {

        serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/saleman/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                	data: null
                },
                {
    	            data: null,
    	            defaultContent: "",
    	            render: function (data, type, full, meta) {
    	            	var id=data.id
    	            	var operate ='<input type="checkbox" class="checkchild">';
    	            	return operate;
    	            }   
                },
                {
                    data: 'salemanName'
                },
                {
                    data: 'gender'
                },
                {
                    data: 'birday',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data: 'position'
                },
                {
                    data: 'contractPhone'
                },
                {
                    data: 'memo'
                }],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
        
        //删除销售人员
        jQuery('.delBtn').on('click', function () {
     	   var selects=getSelect("#dataTable",1);
     	   var url=window.rootPath+"/ui/saleman/delSaleman";
     	   delSelect(selects,url,serverTable) 
     	});
        
    	//编辑销售人员
        jQuery('.editBtn').on('click', function () {
            var selects=getSelect("#dataTable",1);
     	   if(selects.length>1){
     		   $.springLemonDialog({
     		         content: '只能编辑一条记录',// 确认文本
     		         clickButton: function (sender, modal, index) {// 按钮事件处理
     		             $(this).closeDialog(modal);
     		         }
     		   }); 
     	   }
     	   else if(selects.length==0){
     		   $.springLemonDialog({
     		         content: '请选择要编辑的记录',// 确认文本
     		         clickButton: function (sender, modal, index) {// 按钮事件处理
     		             $(this).closeDialog(modal);
     		         }
     		   }); 
     	   }else if(selects.length==1){
     		var id=selects[0].id;
     		var salemanName=selects[0].salemanName;
     		var gender=selects[0].gender;
     		var birday=selects[0].birday;
     		var position=selects[0].position;
     		var contractPhone=selects[0].contractPhone;
     		var memo=selects[0].memo;
            if (gender == "男") {
                var sex="male";
            }
            if (gender == "女") {
            	var sex="female";
            }
            $(":radio[name='gender'][value='" + sex + "']").prop("checked", "checked");
            jQuery('#id').val(id);
            jQuery('#salemanName').val(salemanName);
            jQuery('#birday').val(moment(birday).format("YYYY-MM-DD"));
            jQuery('#position').val(position);
            jQuery('#contractPhone').val(contractPhone);
            jQuery('#memo').val(memo);
            jQuery('#salemanModal').modal('show'); 
     	   }
     	   
     	});
        
    	//编辑销售人员属性，保存
    	var $btn = jQuery('.saveBtn').on('click', function () {
            if(jQuery("#salemanAtrrForm") && !jQuery("#salemanAtrrForm").Validform({tiptype:3, showAllError:true}).check(false)){
                return;
            };
            var isId=jQuery("#id").val();
            //判断是编辑还是新增
            if(isId == ""){
            	var salemanName = jQuery('#salemanName').val();
            	var option = $("input[type='radio']:checked").val();
                if (option == "male") {
                    jQuery("#gender").val("男");
                }
                if (option == "female") {
                    jQuery("#gender").val("女");
                }
             	var gender = jQuery('#gender').val();
         		var birday = jQuery('#birday').val();
         		var position = jQuery('#position').val();
         		var contractPhone = jQuery('#contractPhone').val();
         		var memo = jQuery('#memo').val();
                var api = window.rootPath + "/api/salemen";
                var d = {
                	"salemanName": salemanName,
                    "gender": gender,
                    "birday": birday,
                    "position": position,
                    "contractPhone": contractPhone,
                    "memo": memo
                };
                jQuery.ajax({
                    type: 'post',
                    url: api,
                    data: JSON.stringify(d),
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    success: function (data) {
                    	jQuery('#salemanModal').modal('hide');
                        serverTable.draw(false);
                    }
                });

            }
            else{
            	var option = $("input[type='radio']:checked").val();
                if (option == "male") {
                    jQuery("#gender").val("男");
                }
                if (option == "female") {
                    jQuery("#gender").val("女");
                }
            	var data={
                        id:jQuery('#id').val(),
                        salemanName:jQuery('#salemanName').val(),
             			gender:jQuery('#gender').val(),
         				birday:jQuery('#birday').val(),
         				position:jQuery('#position').val(),
         				contractPhone:jQuery('#contractPhone').val(),
         				memo:jQuery('#memo').val()
                    };
                    var url=window.rootPath+'/api/salemen/'+data.id;
                    jQuery.ajax({
                        url:encodeURI(url),
                        type:'put',
                        data:JSON.stringify(data),
                        dataType:'json',
                        contentType:'application/json;charset=utf-8',
                        success: function () {
                            jQuery('#salemanModal').modal('hide');
                            serverTable.draw(false);
                        }

                    });
            }
        });
        
    	//查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#keyword').val();
            serverTable.draw();
        })
        //撤销查询
        jQuery('.resetBtn').on("click",function()
        {
            jQuery('#keyword').val('');
            serverData.key ='';
            serverTable.draw();
        });
        
        //列表中的全选
        $("#checkAll").click(function(){
        	var check = $(this).prop("checked");
            $(".checkchild").prop("checked", check);
    	});


    });
})();